class Solution {
public:
    string addStrings(string num1, string num2) {
        int end1 = num1.size() - 1, end2 = num2.size() - 1;
        string retstr;
        int next = 0;
        while (end1 >= 0 || end2 >= 0)
        {
            int tmp1 = 0, tmp2 = 0;
            if (end1 >= 0)
                tmp1 = num1[end1] - '0';
            if (end2 >= 0)
                tmp2 = num2[end2] - '0';
            if (tmp1 + tmp2 + next > 9)
            {
                retstr += (tmp1 + tmp2 + next) % 10 + '0';
                next = (tmp1 + tmp2 + next) / 10;
            }
            else
            {
                retstr += tmp1 + tmp2 + next + '0';
                next = 0;
            }
            end1--;
            end2--;
        }
        if (next)
        {
            retstr += next + '0';
        }
        reverse(retstr.begin(), retstr.end());
        return retstr;
    }
    string mmultin(string num1, string num2, int x)
    {
        string n("0");
        string one("1");
        string ret("0");
        while (n != num1)
        {
            ret = addStrings(ret, num2);
            n = addStrings(n, one);
        }
        while (x)
        {
            ret += '0';
            --x;
        }
        return ret;
    }

    string multiply(string num1, string num2) {
        int end2 = num2.size() - 1;
        string ret("0");
        int n = 0;
        while (end2 >= 0)
        {
            string s(1, num2[end2]);
            ret = addStrings(ret, mmultin(s, num1, n));
            ++n;
            --end2;
        }
        if (ret[0] == '0')
        {
            return "0";
        }
        return ret;
    }
};